package com.kaikai.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaikai.commonutils.R;
import com.kaikai.edu.entity.EduCourse;
import com.kaikai.edu.entity.vo.CourseInfoVo;
import com.kaikai.edu.entity.vo.CoursePublishVo;
import com.kaikai.edu.entity.vo.CourseQuery;
import com.kaikai.edu.service.EduCourseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author yujiankai
 * @since 2021-05-14
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    @ApiOperation(value = "添加课程信息")
    @PostMapping("saveCourseInfo")
    public R saveCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("id", id);
    }

    /**
     * 修改课程信息
     * 1.根据课程id查询课程信息回显
     * 2.根据提交表单修改课程信息
     */
    //根据课程id查询课程信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {

        CourseInfoVo courseInfoVo = courseService.getCourseInfoById(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo form) {
        courseService.updateCourseInfo(form);
        return R.ok();
    }

    //根据id查询发布信息
    @GetMapping("getCoursePublishInfo/{courseId}")
    public R getCoursePublishInfo(@PathVariable String courseId) {
        CoursePublishVo publishInfo = courseService.getCoursePublishInfo(courseId);
        return R.ok().data("publishInfo", publishInfo);
    }

    @GetMapping("updateStatus/{courseId}")
    public R updateStatus(@PathVariable String courseId) {
        EduCourse course = new EduCourse();
        course.setId(courseId);
        course.setStatus("Normal");
        courseService.updateById(course);
        return R.ok();
    }

    //查询所有课程信息
    @GetMapping("getAllCourseInfo/{courseId}")
    public R getAllCourseInfo(@PathVariable String courseId) {
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list", list);
    }

    @ApiOperation(value = "多条件查询课程")
    @PostMapping("getCourseInfoByCondition/{current}/{size}")
    public R getCourseInfoByCondition(@ApiParam(value = "当前页") @PathVariable Long current,
                                      @ApiParam(value = "每页记录数") @PathVariable Long size,
                                      @RequestBody CourseQuery courseQuery) {
        //创建page对象
        Page<EduCourse> coursePage = new Page<>(current, size);

        //构建条件wrapper
        //多条件查询
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();

        if (StringUtils.hasText(title)) {
            wrapper.like("title", title);
            System.out.println(title);
        }
        if (!"".equals(status) && status != null) {

            wrapper.eq("status", status);

        }
        if (StringUtils.hasText(begin)) {
            wrapper.ge("gmt_create", begin);
            System.out.println(begin);
        }
        if (StringUtils.hasText(end)) {
            System.out.println(end);

            wrapper.le("gmt_modified", end);
        }
        //添加时间排序
        wrapper.orderByDesc("gmt_create");


        courseService.page(coursePage, wrapper);

        List<EduCourse> records = coursePage.getRecords();

        long total = coursePage.getTotal();

        return R.ok().data("records", records).data("total", total);

    }

    /**
     * 删除课程
     *  删除描述description中数据
     *  删除章节chapter中数据
     *  删除小节video中数据
     * @param courseId
     * @return
     */

    /**
     * delete * from course
     */
    @GetMapping("deleteCourseByID/{courseId}")
    public R deleteCourseByID(@PathVariable String courseId) {
        Boolean b = courseService.deleteCourseById(courseId);
        if (b) {
            return R.ok();
        }
        return R.error().message("删除失败");
    }

}

